package com.example.testblog.service;

import com.example.testblog.Dao.TypeRepository;
import com.example.testblog.entity.Type;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TypeServiceImpl implements  TypeService {
   @Autowired
    private TypeRepository typeRepository;




    @Transactional
    @Override
    public Page<Type> listType(Pageable pageable) {


        return  typeRepository.findAll(pageable);
    }

    @Override
    @Transactional
    public Type saveType(Type type) {

        return  typeRepository.save(type);

    }

    @Override
    @Transactional
    public Type getType(Long id) {

        return typeRepository.getOne(id) ;
    }



    @Override
    public Type updateType(Long id, Type type) {

           Type type1=typeRepository.getOne(id);
           if (type1==null)
           {
               return  null;
           }

        BeanUtils.copyProperties(type,type1);
        return typeRepository.save(type1);
    }

    @Override
    @Transactional
    public void deleteType(Type id) {


          typeRepository.delete(id);
    }

    @Override
    public List<Type> listType() {
        return  typeRepository.findAll();
    }
}
